package com.ja.openchat.ui.me;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.zxing.integration.android.IntentIntegrator;
import com.ja.openchat.BR;
import com.ja.openchat.R;
import com.ja.openchat.databinding.FragmentMeBinding;
import com.ja.openchat.ui.message.ScanActivity;
import com.ja.openchat.viewmodels.MeViewModel;
import com.ja.openchat.views.AttachCopyPopupView;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.enums.PopupPosition;
import com.zy.openchat.core.model.config.AppConfigInfo;

import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.binding.command.BindingCommand;

public class MeFragment extends BaseFragment<FragmentMeBinding, MeViewModel> {

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_me;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();
    }

    @Override
    public void initView() {
        super.initView();
        binding.ivScan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentIntegrator integrator = new IntentIntegrator(getActivity());
                integrator.setPrompt("");
                integrator.setBeepEnabled(false);
                integrator.setBarcodeImageEnabled(false);
                integrator.setOrientationLocked(false);
                integrator.setCaptureActivity(ScanActivity.class);
                integrator.initiateScan();
            }
        });
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.inviteOnClickCommand = new BindingCommand(() -> {
//            new GoShare.FShareBuilder(getActivity(), App.getInstanse().getPackageName() + ".utilcode.fileprovider")
//                    .setShareSubject(getResources().getString(R.string.app_name))
//                    .setTopTitle(getString(R.string.invite_friend_text))
//                    .setShareContent(getString(R.string.share_person_use_app)+"\n" + UserInfo.getUserInfo().qrString())
//                    .build()
//                    .shareByCustom();
            ToastUtils.showShort(getString(R.string.friend_function_not_open));
        });
        viewModel.mUC.getShowPaymentPasswordDialogEvent().observe(this, maps -> {
            String content = (String) maps.get("content");
            if (!TextUtils.isEmpty(content)) {
                int type = (int) maps.get("type");
                View view = binding.tvNick;
                switch (type) {
                    case 1:
                        view = binding.tvNick;
                        break;
                    case 2:
                        view = binding.tvName;
                        break;
                    case 3:
                        view = binding.tvPhone;
                        break;
                }
                new XPopup.Builder(getActivity()).hasShadowBg(false).atView(view).popupPosition(PopupPosition.Top).asCustom(new AttachCopyPopupView(getActivity(), content)).show();
            }
        });
        AppConfigInfo configInfo = AppConfigInfo.getAppWithoutLoginConfigInfo();

        if(configInfo!=null){
            if(!configInfo.can_see_wallet){
                binding.walletView.setVisibility(View.GONE);
//                binding.walletViewLine.setVisibility(View.GONE);
            }
            int goneBottomNumber = 4;
            if(!configInfo.can_see_blog){
                binding.albumBtn.setVisibility(View.GONE);
                binding.albumBtnLine.setVisibility(View.GONE);
                goneBottomNumber--;
            }
            if(!configInfo.can_see_qr_code){
                binding.qrcodeBtn.setVisibility(View.GONE);
                binding.qrcodeBtnLine.setVisibility(View.GONE);
                binding.myQrcodeBtn.setVisibility(View.GONE);
                goneBottomNumber--;
            }
            binding.lineBottomView.setWeightSum(goneBottomNumber);
            int goneTopNumber = 0;
            if(!configInfo.can_invite_friend){
                goneTopNumber++;
                binding.inviteBtn.setVisibility(View.GONE);
                binding.inviteBtnLine.setVisibility(View.GONE);
            }
            if(!configInfo.can_see_nearby){
                goneTopNumber++;
                binding.nearbyBtn.setVisibility(View.GONE);
                binding.nearbyBtnLine.setVisibility(View.GONE);
            }
            if(!configInfo.can_see_public_group){
                goneTopNumber++;
                binding.publicBtn.setVisibility(View.GONE);
                binding.publicBtnLine.setVisibility(View.GONE);
            }
            if(goneTopNumber == 3){
//                binding.topView.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        viewModel.requestWalletInfo();
    }
}